import { getMd5Sign } from '../../utils/util'
const request = require('../config/index')

/**
 * 实时位置上传
 * @param obj
 */
export const realTimeLocationUpload = (obj: any) => {
    const body = {
        API_Model: 'Local',
        API_Name: 'User_Route_Log_Add',
        Data: {
            lat: obj.lat,
            lng: obj.lng,
            address: obj.address
        }
    }
    const md5Res = getMd5Sign(body)

    return new Promise((resolve, reject) => {
        request
            .post(`/api/doAPI`, body, {
                sign: md5Res
            })
            .then((res: any) => {
                resolve(res.data.data)
            })
            .catch((err: any) => {
                reject(err)
            })
    })
}

/**
 * 根据订单id获取订单信息
 * @param user_openid
 * @param id
 */
export const getOrderById = (user_openid: string, id: string) => {
    const body = {
        API_Model: 'Order',
        API_Name: 'Get_Order_Id',
        Data: {
            user_openid,
            id
        }
    }
    const md5Res = getMd5Sign(body)

    return new Promise((resolve, reject) => {
        request
            .post(`/api/doAPI`, body, {
                sign: md5Res
            })
            .then((res: any) => {
                resolve(res.data.data)
            })
            .catch((err: any) => {
                reject(err)
            })
    })
}

/**
 * 获取公海订单 只有司机和管理员可以访问
 * @param page
 * @param size
 */
export const obtainHighSeasOrders = (page = 1, size = 2) => {
    const body = {
        API_Model: 'Driver',
        API_Name: 'Get_Unaccepted_orders',
        Data: {
            page: page, //分页页码，数字类型
            size: size
        }
    }
    const md5Res = getMd5Sign(body)

    return new Promise((resolve, reject) => {
        request
            .post(`/api/doAPI`, body, {
                sign: md5Res
            })
            .then((res: any) => {
                resolve(res.data)
            })
            .catch((err: any) => {
                reject(err)
            })
    })
}

export const ReOrderSearch = (page, size, type) => {
    const openid = wx.getStorageSync('openid')
    const body = {
        API_Model: 'Driver',
        API_Name: 'Re_Order_Search',
        Data: {
            page: page, //分页页码，数字类型
            size: size,
            type: type,
            user_openid: openid
        }
    }
    console.log(body)
    const md5Res = getMd5Sign(body)
    return new Promise((resolve, reject) => {
        request
            .post(`/api/doAPI`, body, {
                sign: md5Res
            })
            .then((res: any) => {
                console.log(res)
                resolve(res.data)
            })
            .catch((err: any) => {
                reject(err)
            })
    })
}

/**
 * 取消订单
 * @param order_id
 */
export const cancelAnOrder = (order_id: string) => {
    const openid = wx.getStorageSync('openid')
    const body = {
        API_Model: 'Order', //API模块名
        API_Name: 'Order_Cancel', //API方法名
        Data: {
            user_openid: openid, //用户唯一标识
            order_id: order_id //订单ID
        }
    }
    const md5Res = getMd5Sign(body)

    return new Promise((resolve, reject) => {
        request
            .post(`/api/doAPI`, body, {
                sign: md5Res
            })
            .then((res: any) => {
                resolve(res.data.data)
            })
            .catch((err: any) => {
                reject(err)
            })
    })
}

/**
 * 创建订单
 * @param orderInfo 订单信息
 */
export const createOrder = (orderInfo: any) => {
    const openid = wx.getStorageSync('openid')
    const body = {
        API_Model: 'Order', //API模块名
        API_Name: 'Order_Init', //API方法名
        Data: {
            order_string: orderInfo.order_string, //获取两地距离和计算金额接口返回的参数
            order_token: orderInfo.order_token, //获取两地距离和计算金额接口返回的参数
            start_user_name: orderInfo.start_user_name, //发件人姓名
            end_user_name: orderInfo.end_user_name, //收件人姓名
            start_user_phone: orderInfo.start_user_phone, //发件人电话
            end_user_phone: orderInfo.end_user_phone, //收件人电话
            user_openid: openid, //用户openid
            start_address: orderInfo.start_address, //发件人地址
            end_address: orderInfo.end_address, //收件人地址
            coupon_id: orderInfo.coupon_id, //优惠券ID
            people_num: orderInfo.people_num, //跟车人数
            cat_num: orderInfo.cat_num, //宠物数量
            remark: orderInfo.remark //备注信息 非必填
        }
    }
    const md5Res = getMd5Sign(body)

    return new Promise((resolve, reject) => {
        request
            .post(`/api/doAPI`, body, {
                sign: md5Res
            })
            .then((res: any) => {
                resolve(res)
            })
            .catch((err: any) => {
                reject(err)
            })
    })
}
/**
 * 支付订单
 * @param orderInfo 订单信息
 */
export const payOrder = order_id => {
    const body = {
        API_Model: 'Pay', //API模块名
        API_Name: 'Order_Pay', //API方法名
        Data: {
            order_id: order_id
        }
    }
    const md5Res = getMd5Sign(body)
    return new Promise((resolve, reject) => {
        request
            .post(`/api/doAPI`, body, {
                sign: md5Res
            })
            .then((res: any) => {
                resolve(res.data.data)
            })
            .catch((err: any) => {
                reject(err)
            })
    })
}
/**
 * 退款订单
 * @param orderInfo 订单信息
 */
export const cancelAnReOrder = (order_id, order_money) => {
    const openid = wx.getStorageSync('openid')
    const body = {
        API_Model: 'Order', //API模块名
        API_Name: 'Order_RePay', //API方法名
        Data: {
            user_openid: openid, //用户OPENID
            order_id: order_id, //退款订单ID
            ReMoney: order_money, //退款金额
            RePay_Remark: '用户主动申请退款' //退款原因
        }
    }
    const md5Res = getMd5Sign(body)
    return new Promise((resolve, reject) => {
        request
            .post(`/api/doAPI`, body, {
                sign: md5Res
            })
            .then((res: any) => {
                resolve(res.data.data)
            })
            .catch((err: any) => {
                reject(err)
            })
    })
}
/**
 * 抢订单
 * @param orderInfo 订单信息
 */
export const ReceivingOrders = order_id => {
    const body = {
        API_Model: 'Driver', //API模块名
        API_Name: 'Receiving_Orders', //API方法名
        Data: {
            order_id: order_id //退款订单ID
        }
    }
    const md5Res = getMd5Sign(body)
    return new Promise((resolve, reject) => {
        request
            .post(`/api/doAPI`, body, {
                sign: md5Res
            })
            .then((res: any) => {
                resolve(res.data)
            })
            .catch((err: any) => {
                reject(err)
            })
    })
}
/**
 * 修改订单
 * @param orderInfo 订单信息
 */
export const ReOrderSet2 = data => {
    const body = {
        API_Model: 'Driver', //API模块名
        API_Name: 'Re_Order_Set', //API方法名
        Data: data
    }
    const md5Res = getMd5Sign(body)
    return new Promise((resolve, reject) => {
        request
            .post(`/api/doAPI`, body, {
                sign: md5Res
            })
            .then(res => {
                console.log(res)
                resolve(res)
            })
            .catch((err: any) => {
                reject(err)
            })
    })
}
